// var Facility = Facility || {};
Facility = function(scene, camera,stageSize) {
    this.hostInfo = null;
    this.scene = scene;
    this.camera = camera;
    this.curMesh=null;
    this.stageSize=stageSize;
    this.targetRotationOnMouseDown = 0;
    this.targetRotation = 0;
    this.touchPoint=new THREE.Vector3;

};
Facility.prototype.constructor = Facility;

Facility.prototype.test= function() {
    alert('ddd');
};
Facility.prototype.loadStyle= function(geometry, scale, position, rotation, material, name, description, _id,gTypeId,roomId,goodsName,roomType) {
    var styleMesh= this.addMesh(geometry, scale, position, rotation, material, name, description, _id,gTypeId,roomId,goodsName);
    if (gTypeId=='floor')
        styleMesh.roomType=roomType;
    return styleMesh;
};
Facility.prototype.load= function(goodsGeo, goodsPos,onMesh) {
    var me = this;
    var today = new Date();
    var rndId = Math.ceil((today.getTime() * 9301 + 49297) % 233280 / 233280.0 * 511555);
    var gTypeId;
    var loader = new THREE.JSONLoader();
    loader.load(goodsGeo.geometry, function(geometry) {
        var position = new THREE.Vector3;
        position.copy(me.touchPoint);
        var rotation = new THREE.Vector3;
        var hostFacInfo=me.hostInfo;
        gTypeId=goodsGeo.gTypeId;
        // rollOverMaterial = new THREE.MeshBasicMaterial( { color: 0xFF99FF, opacity: 0.5, transparent: true } );
        // matrials=new Array();
        // matrials.push(new THREE.MeshFaceMaterial());
        // matrials.push(rollOverMaterial);

        if(goodsPos) {
            position.set(parseInt(goodsPos.position.x), parseInt(goodsPos.position.y), parseInt(goodsPos.position.z));
            rotation.set(parseInt(goodsPos.rotation.x), parseInt(goodsPos.rotation.y), parseInt(goodsPos.rotation.z));
            me.curMesh=me.addMesh(geometry, 1, position, rotation, new THREE.MeshFaceMaterial(), "room",goodsGeo.description, goodsGeo._id,goodsGeo.gTypeId,hostFacInfo.roomId,goodsGeo.goodsName);
            // me.curMesh.name = hostFacInfo.rndId;
        } else {

            me.curMesh=me.addMesh(geometry, 1, position, rotation, new THREE.MeshFaceMaterial(), "room", goodsGeo.description, goodsGeo._id,goodsGeo.gTypeId,hostFacInfo.roomId,goodsGeo.goodsName);

            if(me.curMesh.gTypeId!="10") {

                switch(hostFacInfo.parasitizedFace.gTypeId) {
                    case 'fwall':
                        position.set(hostFacInfo.parasitizedFace.position.x, hostFacInfo.parasitizedFace.position.y, hostFacInfo.parasitizedFace.position.z + me.curMesh.size.z / 2);
                        break;
                    case 'bwall':
                        position.set(hostFacInfo.parasitizedFace.position.x, hostFacInfo.parasitizedFace.position.y, hostFacInfo.parasitizedFace.position.z - me.curMesh.size.z / 2);
                        break;
                    case 'lwall':
                        position.set(hostFacInfo.parasitizedFace.position.x + me.curMesh.size.x / 2, hostFacInfo.parasitizedFace.position.y, hostFacInfo.parasitizedFace.position.z);
                        break;
                    case 'rwall':
                        position.set(hostFacInfo.parasitizedFace.position.x - me.curMesh.size.x / 2, hostFacInfo.parasitizedFace.position.y, hostFacInfo.parasitizedFace.position.z);
                        break;
                    case 'floor':
                        position.set(hostFacInfo.parasitizedFace.position.x, hostFacInfo.parasitizedFace.position.y + me.curMesh.size.y / 2, hostFacInfo.parasitizedFace.position.z);
                        break;
                    case 'ceiling':
                        position.set(hostFacInfo.parasitizedFace.position.x, hostFacInfo.parasitizedFace.position.y - me.curMesh.size.y / 2, hostFacInfo.parasitizedFace.position.z);
                        break;
                }
            } else {
                position=hostFacInfo.parasitizedFace.position;

            }
            me.curMesh.position.copy(position);
            if(me.curMesh.name!="room") {
                var geo = new THREE.CubeGeometry(parseInt(me.curMesh.size.x), parseInt(me.curMesh.size.y), parseInt(me.curMesh.size.z));
                var mat = new THREE.MeshBasicMaterial( { color: 0xff0000, opacity: 0.5, transparent: true } );
                me.mesh=new THREE.Mesh(geo, mat);
                me.curMesh.add(me.mesh);
                me.mesh.visible=false;
            }

            me.setFocuse();
            $('canvas').on('mousemove', function(event) {
                var mouse2D = new THREE.Vector3( 0, 0, 0.5 );
                mouse2D.x = (event.clientX - RoomUtil.OFFSET_SIZE.x)/ ($('canvas').width()) * 2 - 1;
                mouse2D.y = -((event.clientY - RoomUtil.OFFSET_SIZE.y) / ($('canvas').height())) * 2 + 1;
                
                me.move(mouse2D,onMesh);
                
                $('canvas').off('click');
                $('canvas').click(function(event){
                  $('canvas').off('mousemove');
                    $('canvas').on('click',function(event){
                       me.setFocuse(event);
                    });
                    // me.setFocuse(event);                    
                });
            });
        }
        // me.curMesh.parasitizedFace=hostFacInfo.parasitizedFace;
        // me.curMesh.parasitizedFace={x:parseInt(hostFacInfo.parasitizedFace.x),y:parseInt(hostFacInfo.parasitizedFace.y),z:parseInt(hostFacInfo.parasitizedFace.z)};
        me.curMesh.parasitizedFace = {
            "gTypeId" : hostFacInfo.parasitizedFace.gTypeId,
            "position" : {
                "x" : parseInt(hostFacInfo.parasitizedFace.position.x),
                "y" : parseInt(hostFacInfo.parasitizedFace.position.y),
                "z" : parseInt(hostFacInfo.parasitizedFace.position.z)
            },
            "size" : {
                "x" : parseInt(hostFacInfo.parasitizedFace.size.x),
                "y" : parseInt(hostFacInfo.parasitizedFace.size.y),
                "z" : parseInt(hostFacInfo.parasitizedFace.size.z)
            }
        };
         this.scene.add(me.curMesh);
        // var geo = new THREE.CubeGeometry(parseInt(me.curMesh.size.x), parseInt(me.curMesh.size.y), parseInt(me.curMesh.size.z));
        // var material= new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff } );
        // me.tmp=me.addMesh(geo, 1, me.curMesh.position, me.curMesh.rotation, material, "name", "description", "_id","gTypeId","roomId","test");
    });
    // this.touchPoint=new THREE.Vector3;
};
Facility.prototype.setFocuseStructure=function(event){
	        event.preventDefault();
        var mouse2D = new THREE.Vector3(0, 0,0.5);
        mouse2D.x = ((event.clientX - RoomUtil.OFFSET_SIZE.x)/ $('canvas').width()) * 2 - 1;
        mouse2D.y = -((event.clientY - RoomUtil.OFFSET_SIZE.y) / ($('canvas').height())) * 2 + 1;
        projector = new THREE.Projector();
        ray = projector.pickingRay(mouse2D.clone(), this.camera);
        
        for(var i=0;i<this.scene.children.length;i++){
        	if(this.scene.children[i].name=='group'){
        	var room=this.scene.children[i];
        	var intersects = ray.intersectObjects(room.children);
        	var parasitizedFace = null;
        	description = null, rndId = null;
        	if(intersects.length > 0) {
	            tor = null;
	
	            var intersect = intersects[0];
	            this.curMesh = intersect.object.parent;
	            var tmpVec=new THREE.Vector3;
	            tmpVec.copy( intersect.face.normal );
	
	            this.touchPoint.add( intersect.point, intersect.object.matrixRotationWorld.multiplyVector3( tmpVec ) );
	
	            this.touchPoint.x = Math.floor( this.touchPoint.x / 50 ) * 50 + 25;
	            this.touchPoint.y = Math.floor( this.touchPoint.y / 50 ) * 50 + 25;
	            this.touchPoint.z = Math.floor( this.touchPoint.z / 50 ) * 50 + 25;

        	}
        	break; 
        	}
        }
    	
	    this.hostInfo=this.curMesh.detail;
	    var me = this;
	    $('canvas').off('mousemove');	
	    $('.facilityPanel').load(structurePopmenuUrl, {
	        'hostInfo' : JSON.stringify(this.curMesh.detail)
	    }, function() {
	        // me.showStructurePanel(me.curMesh.detail);
	    }).draggable({
	        cursor : 'crosshair',
	        handle : ".draghandle",
	        containment : '.page',
	        scroll : false
	    });

};
Facility.prototype.setFocuse = function(event) {
    if(event) {

        event.preventDefault();
        var mouse2D = new THREE.Vector3(0, 0,0.5);
        mouse2D.x = ((event.clientX - RoomUtil.OFFSET_SIZE.x)/ $('canvas').width()) * 2 - 1;
        mouse2D.y = -((event.clientY - RoomUtil.OFFSET_SIZE.y) / ($('canvas').height())) * 2 + 1;
        projector = new THREE.Projector();
        ray = projector.pickingRay(mouse2D.clone(), this.camera);
        var intersects=[];
        // for(var i=0;i<this.scene.children.length;i++){
          // if(this.scene.children[i].name=='roomGroup'||this.scene.children[i].name=='structure3D'){ 
        	// var room=this.scene.children[i];
        	// intersects = ray.intersectObjects(room.children);
		  // }
			// var parasitizedFace = null;
        	// description = null, rndId = null;
        	// if(intersects.length > 0) {
            	// tor = null;
            // var intersect = intersects[0];
            // this.curMesh = intersect.object;
            // var tmpVec=new THREE.Vector3;
            // tmpVec.copy( intersect.face.normal );
            // this.touchPoint.add( intersect.point, intersect.object.matrixRotationWorld.multiplyVector3( tmpVec ) );
            // this.touchPoint.x = Math.floor( this.touchPoint.x / RoomUtil.CELL_SIZE ) * RoomUtil.CELL_SIZE + RoomUtil.CELL_SIZE/2;
            // this.touchPoint.y = Math.floor( this.touchPoint.y / RoomUtil.CELL_SIZE ) * RoomUtil.CELL_SIZE + RoomUtil.CELL_SIZE/2;
            // this.touchPoint.z = Math.floor( this.touchPoint.z / RoomUtil.CELL_SIZE ) * RoomUtil.CELL_SIZE + RoomUtil.CELL_SIZE/2;
        // } 
        // }
        
  
    
    	intersects = ray.intersectObjects(this.scene.children);
		var parasitizedFace = null;
        	description = null, rndId = null;
        	if(intersects.length > 0) {
            	tor = null;

            var intersect = intersects[0];
            this.curMesh = intersect.object;
            var tmpVec=new THREE.Vector3;
            tmpVec.copy( intersect.face.normal );

            this.touchPoint.add( intersect.point, intersect.object.matrixRotationWorld.multiplyVector3( tmpVec ) );

            this.touchPoint.x = Math.floor( this.touchPoint.x / RoomUtil.CELL_SIZE ) * RoomUtil.CELL_SIZE + RoomUtil.CELL_SIZE/2;
            this.touchPoint.y = Math.floor( this.touchPoint.y / RoomUtil.CELL_SIZE ) * RoomUtil.CELL_SIZE + RoomUtil.CELL_SIZE/2;
            this.touchPoint.z = Math.floor( this.touchPoint.z / RoomUtil.CELL_SIZE ) * RoomUtil.CELL_SIZE + RoomUtil.CELL_SIZE/2;

        }  
    
      }
    
    var gTypeId = this.curMesh.gTypeId;
    var description = this.curMesh.description;
    var rndId = this.curMesh.rndId;
    var goodsName = this.curMesh.goodsName;
    var roomId = this.curMesh.roomId;
    var parasitizedFace = null;
    if(this.curMesh.gTypeId.substring(0, 2) == '05') {
        //gTypId的前两位是‘05’，是桌子，台面可放物品。
        parasitizedFace = {
            "gTypeId" : this.curMesh.gTypeId,
            "position" : {
                "x" : this.curMesh.position.x,
                "y" : this.curMesh.position.y + this.curMesh.size.y / 2,
                "z" : this.curMesh.position.z
            },
            "size" : {
                "x" : this.curMesh.size.x,
                "y" : 0,
                "z" : this.curMesh.size.z
            }
        };
    } else {
        //对四面墙、天花板、地板的设置。

        switch(this.curMesh.gTypeId) {
        	case 'structure':
                parasitizedFace = {
                    "gTypeId" : this.curMesh.gTypeId,
                    "position" : {
                        "x" : this.curMesh.position.x,
                        "y" : this.curMesh.position.y + this.curMesh.size.y / 2,
                        "z" : this.curMesh.position.z
                    },
                    "size" : {
                        "x" : this.curMesh.size.x,
                        "y" : 0,
                        "z" : this.curMesh.size.z
                    }
                };
                break;
            case 'floor':
                parasitizedFace = {
                    "gTypeId" : this.curMesh.gTypeId,
                    "position" : {
                        "x" : this.curMesh.position.x,
                        "y" : this.curMesh.position.y + this.curMesh.size.y / 2,
                        "z" : this.curMesh.position.z
                    },
                    "size" : {
                        "x" : this.curMesh.size.x,
                        "y" : 0,
                        "z" : this.curMesh.size.z
                    }
                };
                break;
            case 'ceiling':
                parasitizedFace = {
                    "gTypeId" : this.curMesh.gTypeId,
                    "position" : {
                        "x" : this.curMesh.position.x,
                        "y" : this.curMesh.position.y - this.curMesh.size.y / 2,
                        "z" : this.curMesh.position.z
                    },
                    "size" : {
                        "x" : this.curMesh.size.x,
                        "y" : 0,
                        "z" : this.curMesh.size.z
                    }
                };
                break;
            case 'xwall':
                parasitizedFace = {
                    "gTypeId" : this.curMesh.gTypeId,
                    "position" : {
                        "x" : this.curMesh.position.x,
                        "y" : this.curMesh.position.y,
                        "z" : this.curMesh.position.z + this.curMesh.size.z / 2
                    },
                    "size" : {
                        "x" : this.curMesh.size.x,
                        "y" : this.curMesh.size.y,
                        "z" : this.curMesh.size.z
                    }
                };
                break;
            case 'ywall':
                parasitizedFace = {
                    "gTypeId" : this.curMesh.gTypeId,
                    "position" : {
                        "x" : this.curMesh.position.x + this.curMesh.size.x / 2,
                        "y" : this.curMesh.position.y,
                        "z" : this.curMesh.position.z
                    },
                    "size" : {
                        "x" : this.curMesh.size.x,
                        "y" : this.curMesh.size.y,
                        "z" : this.curMesh.size.z
                    }
                };
                break;
            case 'fwall':
                parasitizedFace = {
                    "gTypeId" : this.curMesh.gTypeId,
                    "position" : {
                        "x" : this.curMesh.position.x,
                        "y" : this.curMesh.position.y,
                        "z" : this.curMesh.position.z + this.curMesh.size.z / 2
                    },
                    "size" : {
                        "x" : this.curMesh.size.x,
                        "y" : this.curMesh.size.y,
                        "z" : this.curMesh.size.z
                    }
                };
                break;
            case 'bwall':
                parasitizedFace = {
                    "gTypeId" : this.curMesh.gTypeId,
                    "position" : {
                        "x" : this.curMesh.position.x,
                        "y" : this.curMesh.position.y,
                        "z" : this.curMesh.position.z - this.curMesh.size.z / 2
                    },
                    "size" : {
                        "x" : this.curMesh.size.x,
                        "y" : this.curMesh.size.y,
                        "z" : this.curMesh.size.z
                    }
                };
                break;
            case 'lwall':
                parasitizedFace = {
                    "gTypeId" : this.curMesh.gTypeId,
                    "position" : {
                        "x" : this.curMesh.position.x + this.curMesh.size.x / 2,
                        "y" : this.curMesh.position.y,
                        "z" : this.curMesh.position.z
                    },
                    "size" : {
                        "x" : this.curMesh.size.x,
                        "y" : this.curMesh.size.y,
                        "z" : this.curMesh.size.z
                    }
                };
                break;
            case 'rwall':
                parasitizedFace = {
                    "gTypeId" : this.curMesh.gTypeId,
                    "position" : {
                        "x" : this.curMesh.position.x - this.curMesh.size.x / 2,
                        "y" : this.curMesh.position.y,
                        "z" : this.curMesh.position.z
                    },
                    "size" : {
                        "x" : this.curMesh.size.x,
                        "y" : this.curMesh.size.y,
                        "z" : this.curMesh.size.z
                    }
                };
                break;
        }

        this.loadPanel({
            "description" : description,
            "parasitizedFace" : parasitizedFace,
            "rndId" : rndId,
            "gTypeId" : gTypeId,
            "name" : goodsName,
            "roomId" : roomId
        });
        // return {name:name,description:description,parasitizedFace:parasitizedFace};
    }
    // return null;
    // console.warn(this.curMesh);
};
Facility.prototype.loadPanel= function(hostInfo) {

    // console.warn(hostInfo);
    this.hostInfo=hostInfo;
    var me = this;
    $('canvas').off('mousemove');
    
    $('.facilityPanel').load(popmenuUrl, {
        'hostInfo' : JSON.stringify(hostInfo)
    }, function() {
        // me.showPanel(hostInfo);
        console.warn(hostInfo);
    }).draggable({
        cursor : 'crosshair',
        handle : ".draghandle",
        containment : '.page',
        scroll : false
    });

};

Facility.prototype.showStructurePanel=function(details){
	// if(details[0].gTypeId=='floor'){
		// $('.waposition.ynu').show();
        // $('.opmenu').hide();
	// }
};
Facility.prototype.showPanel= function(hostInfo) {
    var me = this;

    if(hostInfo.gTypeId=='fwall'
    ||hostInfo.gTypeId=='bwall'
    ||hostInfo.gTypeId=='lwall'
    ||hostInfo.gTypeId=='rwall'
    ||hostInfo.gTypeId=='xwall'
    ||hostInfo.gTypeId=='ywall'
    ||hostInfo.gTypeId=='floor'
    ||hostInfo.gTypeId=='ceiling') {
        $('.waposition.ynu').show();
        // $('.opmenu').hide();
        $('#picker').farbtastic($('#color'));
        var children = this.scene.children;

        $('.marker').mouseup( function() {
            me.paint($('#color').val(), true, 0);
        });
    } else {
        // $('.wallmenu').hide();
        $('.opmenu').show();
        $("#move").on('click', function() {
            $('canvas').off('click');
            $('canvas').click( function() {
                $('canvas').off('mousemove');

            });
            $('canvas').on('mousemove', function(event) {
                var mouse2D = new THREE.Vector3( 0, 0, 0.5 );
                // mouse2D.x = ( event.clientX / me.stageSize.width ) * 2 - 1;
                // mouse2D.y = - ( event.clientY / me.stageSize.height ) * 2 + 1;
                mouse2D.x = (event.clientX / $('canvas').width()) * 2 - 1;
                mouse2D.y = -((event.clientY - RoomUtil.OFFSET_SIZE.y) / ($('canvas').height())) * 2 + 1;
                me.move(mouse2D);
            });
            // $('canvas').mousemove(function(){me.move();});
        });
        $("#remove").on('click', function() {
            me.remove();
        });
        $("#rotate").on('click', function() {
            me.rotate(mouse2D);
        });
        $("#replace").on('click', function() {
            me.replace();
        });
    }

};
Facility.prototype.replace= function() {
    alert('ddd');
};
/**
 * paintType 0,color;1,wall paper
 */

Facility.prototype.paint= function(cOrw, single, paintType) {
    var children = this.scene.children;
    if(single) {
        // var wallMesh = this.scene.getChildByName(wallName);
        // for(var c = 0, cl = children.length; c < cl; c++) {
        // var child = children[c];
        // if(child.isFocused) {
        // this.paintSingle(child, cOrw, paintType);
        // return;
        // }
        // }
        this.paintSingle(this.curMesh,cOrw,paintType);
    } else {
        for(var c = 0, cl = children.length; c < cl; c++) {
            var child = children[c];
            if(child.parTypeId === 'wall') {
                this.paintSingle(child, cOrw, paintType);
            }
        }
    }

};
Facility.prototype.paintSingle= function(wallMesh, cOrw, paintType) {

    switch(paintType) {
        case 0:
            var xColor = cOrw.replace('#', '0x');
            // wallMesh.material = new THREE.MeshLambertMaterial({
            // color : xColor/*, opacity: 1, shading: THREE.FlatShading */
            // });
            wallMesh.material = new THREE.MeshBasicMaterial({
                color : xColor
            });
            
            // wallMesh.material.map=null;
            
            // wallMesh.material.color=xColor;
            // 墙的gid存储的是color
            wallMesh.gid=xColor;
            break;
        case 1:
            // 有的jpg文件贴图时会产生错误。
            var texture = THREE.ImageUtils.loadTexture( cOrw );

            texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
            texture.image.width=RoomUtil.TEXTURE_WIDTH;
            texture.image.height=RoomUtil.TEXTURE_HEIGHT;

            if(wallMesh.gTypeId=="xwall") {
                var reX=parseInt(wallMesh.size.x/texture.image.width)>1?parseInt(wallMesh.size.x/texture.image.width):1;
                var reY=parseInt(wallMesh.size.y/texture.image.height)>1?parseInt(wallMesh.size.y/texture.image.height):1;
                texture.repeat.set( reX, reY);
                wallMesh.material.map=texture;
            } else if(wallMesh.gTypeId=="floor"||wallMesh.gTypeId=='ceiling') {
                var reX=parseInt(wallMesh.size.x/texture.image.width)>1?parseInt(wallMesh.size.x/texture.image.width):1;
                var reY=parseInt(wallMesh.size.z/texture.image.height)>1?parseInt(wallMesh.size.z/texture.image.height):1;
                texture.repeat.set( reX, reY);
                wallMesh.material.map=texture;
            } else if (wallMesh.gTypeId=="ywall") {
                var reX=parseInt(wallMesh.size.z/texture.image.width)>1?parseInt(wallMesh.size.z/texture.image.width):1;
                var reY=parseInt(wallMesh.size.y/texture.image.height)>1?parseInt(wallMesh.size.y/texture.image.height):1;
                texture.repeat.set( reX,reY);
                wallMesh.material.map=texture;
            } else if(wallMesh.gTypeId=="structure"||wallMesh.gTypeId=='structure') {
                var reX=parseInt(wallMesh.size.x/texture.image.width)>1?parseInt(wallMesh.size.x/texture.image.width):1;
                var reY=parseInt(wallMesh.size.z/texture.image.height)>1?parseInt(wallMesh.size.z/texture.image.height):1;
                texture.repeat.set( reX, reY);
                wallMesh.material.map=texture;
            }
            wallMesh.gid=cOrw;
            break;
    }

};
Facility.prototype.rotate= function(mouse2D) {
    var me=this;
    $('canvas').bind('mousedown', function(event) {
        me.rotateMouseDown(event);
    });
};
Facility.prototype.rotateMouseDown= function ( event ) {
    var me = this;
    event.preventDefault();
    $('canvas').off('mousemove');
    $('canvas').off('click');

    this.mouseXOnMouseDown = event.clientX - windowHalfX;
    this.targetRotationOnMouseDown = this.targetRotation;
    this.curMesh.isRotate = true;

    $('canvas').bind('mousemove', function(event) {
        me.rotateMouseMove(event);
    });
    $('canvas').bind('mouseup', function(event) {
        me.rotateMouseUp(event);
    });
    $('canvas').bind('mouseout', function(event) {
        me.rotateMouseOut(event);
    });
};
Facility.prototype.rotateMouseMove= function( event ) {
    mouseX = event.clientX - windowHalfX;
    this.targetRotation = this.targetRotationOnMouseDown + ( mouseX - this.mouseXOnMouseDown ) * 0.02;
};
Facility.prototype.rotateMouseUp= function ( event ) {
    var me=this;
    $('canvas').unbind( 'mousemove');
    $('canvas').unbind( 'mouseup');
    $('canvas').unbind( 'mouseout');
    $('canvas').unbind( 'mousedown');
    $('canvas').bind('click', function(event) {
        me.setFocuse(event);
    });
    this.curMesh.isRotate=false;
};
Facility.prototype.rotateMouseOut= function( event ) {
    var me=this;
    $('canvas').bind( 'mousemove', function( event ) {
        me.rotateMouseMove(event);
    } );
    $('canvas').bind( 'mouseup', function( event ) {
        me.rotateMouseUp(event);
    } );
    $('canvas').bind( 'mouseout', function( event ) {
        me.rotateMouseOut(event);
    } );
};
// Facility.prototype.move=function(mouse2D){
// var me=this;
// this.locateOnHost(mouse2D);
// if(this.parasitizedPoint){
// this.curMesh.position=this.parasitizedPoint;
// this.curMesh.position.y+=5;
// }
// }
/**
 * 具有碰撞功能的move,根据依靠。
 */
Facility.prototype.move= function(mouse2D,onMesh) {
    var me=this;
    this.locateOnHost(mouse2D,onMesh);
    if(this.parasitizedPoint) {

        switch(this.curMesh.parasitizedFace.gTypeId) {
            case 'fwall':
                this.parasitizedPoint.z=this.curMesh.parasitizedFace.position.z+this.curMesh.size.z/2;
                if(this.parasitizedPoint.x+this.curMesh.size.x/2>this.curMesh.parasitizedFace.position.x+this.curMesh.parasitizedFace.size.x/2) {
                    this.parasitizedPoint.x=this.curMesh.parasitizedFace.position.x+this.curMesh.parasitizedFace.size.x/2-this.curMesh.size.x/2;
                } else if(this.parasitizedPoint.x-this.curMesh.size.x/2<this.curMesh.parasitizedFace.position.x-this.curMesh.parasitizedFace.size.x/2) {
                    this.parasitizedPoint.x=this.curMesh.parasitizedFace.position.x-this.curMesh.parasitizedFace.size.x/2+this.curMesh.size.x/2;
                }
                if(this.parasitizedPoint.y+this.curMesh.size.y/2>this.curMesh.parasitizedFace.position.y+this.curMesh.parasitizedFace.size.y/2) {
                    this.parasitizedPoint.y=this.curMesh.parasitizedFace.position.y+this.curMesh.parasitizedFace.size.y/2-this.curMesh.size.y/2;
                } else if(this.parasitizedPoint.y-this.curMesh.size.y/2<this.curMesh.parasitizedFace.position.y-this.curMesh.parasitizedFace.size.y/2) {
                    this.parasitizedPoint.y=this.curMesh.parasitizedFace.position.y-this.curMesh.parasitizedFace.size.y/2+this.curMesh.size.y/2;
                }
                // hasInsectorOnXY();
                break;
            case 'bwall':
                this.parasitizedPoint.z=this.curMesh.parasitizedFace.position.z-this.curMesh.size.z/2;
                if(this.parasitizedPoint.x+this.curMesh.size.x/2>this.curMesh.parasitizedFace.position.x+this.curMesh.parasitizedFace.size.x/2) {
                    this.parasitizedPoint.x=this.curMesh.parasitizedFace.position.x+this.curMesh.parasitizedFace.size.x/2-this.curMesh.size.x/2;
                } else if(this.parasitizedPoint.x-this.curMesh.size.x/2<this.curMesh.parasitizedFace.position.x-this.curMesh.parasitizedFace.size.x/2) {
                    this.parasitizedPoint.x=this.curMesh.parasitizedFace.position.x-this.curMesh.parasitizedFace.size.x/2+this.curMesh.size.x/2;
                }
                if(this.parasitizedPoint.y+this.curMesh.size.y/2>this.curMesh.parasitizedFace.position.y+this.curMesh.parasitizedFace.size.y/2) {
                    this.parasitizedPoint.y=this.curMesh.parasitizedFace.position.y+this.curMesh.parasitizedFace.size.y/2-this.curMesh.size.y/2;
                } else if(this.parasitizedPoint.y-this.curMesh.size.y/2<this.curMesh.parasitizedFace.position.y-this.curMesh.parasitizedFace.size.y/2) {
                    this.parasitizedPoint.y=this.curMesh.parasitizedFace.position.y-this.curMesh.parasitizedFace.size.y/2+this.curMesh.size.y/2;
                }
                break;
            case 'lwall':
                this.parasitizedPoint.x=this.curMesh.parasitizedFace.position.x+this.curMesh.size.x/2;
                if(this.parasitizedPoint.z+this.curMesh.size.z/2>this.curMesh.parasitizedFace.position.z+this.curMesh.parasitizedFace.size.z/2) {
                    this.parasitizedPoint.z=this.curMesh.parasitizedFace.position.z+this.curMesh.parasitizedFace.size.z/2-this.curMesh.size.z/2;
                } else if(this.parasitizedPoint.z-this.curMesh.size.z/2<this.curMesh.parasitizedFace.position.z-this.curMesh.parasitizedFace.size.z/2) {
                    this.parasitizedPoint.z=this.curMesh.parasitizedFace.position.z-this.curMesh.parasitizedFace.size.z/2+this.curMesh.size.z/2;
                }
                if(this.parasitizedPoint.y+this.curMesh.size.y/2>this.curMesh.parasitizedFace.position.y+this.curMesh.parasitizedFace.size.y/2) {
                    this.parasitizedPoint.y=this.curMesh.parasitizedFace.position.y+this.curMesh.parasitizedFace.size.y/2-this.curMesh.size.y/2;
                } else if(this.parasitizedPoint.y-this.curMesh.size.y/2<this.curMesh.parasitizedFace.position.y-this.curMesh.parasitizedFace.size.y/2) {
                    this.parasitizedPoint.y=this.curMesh.parasitizedFace.position.y-this.curMesh.parasitizedFace.size.y/2+this.curMesh.size.y/2;
                }
                break;
            case 'rwall':
                this.parasitizedPoint.x=this.curMesh.parasitizedFace.position.x-this.curMesh.size.x/2;
                if(this.parasitizedPoint.z+this.curMesh.size.z/2>this.curMesh.parasitizedFace.position.z+this.curMesh.parasitizedFace.size.z/2) {
                    this.parasitizedPoint.z=this.curMesh.parasitizedFace.position.z+this.curMesh.parasitizedFace.size.z/2-this.curMesh.size.z/2;
                } else if(this.parasitizedPoint.z-this.curMesh.size.z/2<this.curMesh.parasitizedFace.position.z-this.curMesh.parasitizedFace.size.z/2) {
                    this.parasitizedPoint.z=this.curMesh.parasitizedFace.position.z-this.curMesh.parasitizedFace.size.z/2+this.curMesh.size.z/2;
                }
                if(this.parasitizedPoint.y+this.curMesh.size.y/2>this.curMesh.parasitizedFace.position.y+this.curMesh.parasitizedFace.size.y/2) {
                    this.parasitizedPoint.y=this.curMesh.parasitizedFace.position.y+this.curMesh.parasitizedFace.size.y/2-this.curMesh.size.y/2;
                } else if(this.parasitizedPoint.y-this.curMesh.size.y/2<this.curMesh.parasitizedFace.position.y-this.curMesh.parasitizedFace.size.y/2) {
                    this.parasitizedPoint.y=this.curMesh.parasitizedFace.position.y-this.curMesh.parasitizedFace.size.y/2+this.curMesh.size.y/2;
                }
                break;
            case 'floor':
                this.parasitizedPoint.y=this.curMesh.parasitizedFace.position.y+this.curMesh.size.y/2;
                if(this.parasitizedPoint.z+this.curMesh.size.z/2>this.curMesh.parasitizedFace.position.z+this.curMesh.parasitizedFace.size.z/2) {
                    this.parasitizedPoint.z=this.curMesh.parasitizedFace.position.z+this.curMesh.parasitizedFace.size.z/2-this.curMesh.size.z/2;
                } else if(this.parasitizedPoint.z-this.curMesh.size.z/2<this.curMesh.parasitizedFace.position.z-this.curMesh.parasitizedFace.size.z/2) {
                    this.parasitizedPoint.z=this.curMesh.parasitizedFace.position.z-this.curMesh.parasitizedFace.size.z/2+this.curMesh.size.z/2;
                }
                if(this.parasitizedPoint.x+this.curMesh.size.x/2>this.curMesh.parasitizedFace.position.x+this.curMesh.parasitizedFace.size.x/2) {
                    this.parasitizedPoint.x=this.curMesh.parasitizedFace.position.x+this.curMesh.parasitizedFace.size.x/2-this.curMesh.size.x/2;
                } else if(this.parasitizedPoint.x-this.curMesh.size.x/2<this.curMesh.parasitizedFace.position.x-this.curMesh.parasitizedFace.size.x/2) {
                    this.parasitizedPoint.x=this.curMesh.parasitizedFace.position.x-this.curMesh.parasitizedFace.size.x/2+this.curMesh.size.x/2;
                }
                break;
            case 'ceiling':
                this.parasitizedPoint.y=this.curMesh.parasitizedFace.position.y-this.curMesh.size.y/2;
                if(this.parasitizedPoint.z+this.curMesh.size.z/2>this.curMesh.parasitizedFace.position.z+this.curMesh.parasitizedFace.size.z/2) {
                    this.parasitizedPoint.z=this.curMesh.parasitizedFace.position.z+this.curMesh.parasitizedFace.size.z/2-this.curMesh.size.z/2;
                } else if(this.parasitizedPoint.z-this.curMesh.size.z/2<this.curMesh.parasitizedFace.position.z-this.curMesh.parasitizedFace.size.z/2) {
                    this.parasitizedPoint.z=this.curMesh.parasitizedFace.position.z-this.curMesh.parasitizedFace.size.z/2+this.curMesh.size.z/2;
                }
                if(this.parasitizedPoint.x+this.curMesh.size.x/2>this.curMesh.parasitizedFace.position.x+this.curMesh.parasitizedFace.size.x/2) {
                    this.parasitizedPoint.x=this.curMesh.parasitizedFace.position.x+this.curMesh.parasitizedFace.size.x/2-this.curMesh.size.x/2;
                } else if(this.parasitizedPoint.x-this.curMesh.size.x/2<this.curMesh.parasitizedFace.position.x-this.curMesh.parasitizedFace.size.x/2) {
                    this.parasitizedPoint.x=this.curMesh.parasitizedFace.position.x-this.curMesh.parasitizedFace.size.x/2+this.curMesh.size.x/2;
                }
                break;
            case 'structure':
                this.parasitizedPoint.y=this.curMesh.parasitizedFace.position.y+this.curMesh.size.y/2;
                if(this.parasitizedPoint.z+this.curMesh.size.z/2>this.curMesh.parasitizedFace.position.z+this.curMesh.parasitizedFace.size.z/2) {
                    this.parasitizedPoint.z=this.curMesh.parasitizedFace.position.z+this.curMesh.parasitizedFace.size.z/2-this.curMesh.size.z/2;
                } else if(this.parasitizedPoint.z-this.curMesh.size.z/2<this.curMesh.parasitizedFace.position.z-this.curMesh.parasitizedFace.size.z/2) {
                    this.parasitizedPoint.z=this.curMesh.parasitizedFace.position.z-this.curMesh.parasitizedFace.size.z/2+this.curMesh.size.z/2;
                }
                if(this.parasitizedPoint.x+this.curMesh.size.x/2>this.curMesh.parasitizedFace.position.x+this.curMesh.parasitizedFace.size.x/2) {
                    this.parasitizedPoint.x=this.curMesh.parasitizedFace.position.x+this.curMesh.parasitizedFace.size.x/2-this.curMesh.size.x/2;
                } else if(this.parasitizedPoint.x-this.curMesh.size.x/2<this.curMesh.parasitizedFace.position.x-this.curMesh.parasitizedFace.size.x/2) {
                    this.parasitizedPoint.x=this.curMesh.parasitizedFace.position.x-this.curMesh.parasitizedFace.size.x/2+this.curMesh.size.x/2;
                }
                break;
                
			case 'xwall':
				if(this.curMesh.position.z>this.curMesh.parasitizedFace.position.z){
					this.parasitizedPoint.z=this.curMesh.parasitizedFace.position.z+this.curMesh.size.z/2;
                	if(this.parasitizedPoint.x+this.curMesh.size.x/2>this.curMesh.parasitizedFace.position.x+this.curMesh.parasitizedFace.size.x/2) {
                    	this.parasitizedPoint.x=this.curMesh.parasitizedFace.position.x+this.curMesh.parasitizedFace.size.x/2-this.curMesh.size.x/2;
                	} else if(this.parasitizedPoint.x-this.curMesh.size.x/2<this.curMesh.parasitizedFace.position.x-this.curMesh.parasitizedFace.size.x/2) {
                    	this.parasitizedPoint.x=this.curMesh.parasitizedFace.position.x-this.curMesh.parasitizedFace.size.x/2+this.curMesh.size.x/2;
                	}
                	if(this.parasitizedPoint.y+this.curMesh.size.y/2>this.curMesh.parasitizedFace.position.y+this.curMesh.parasitizedFace.size.y/2) {
                    	this.parasitizedPoint.y=this.curMesh.parasitizedFace.position.y+this.curMesh.parasitizedFace.size.y/2-this.curMesh.size.y/2;
                	} else if(this.parasitizedPoint.y-this.curMesh.size.y/2<this.curMesh.parasitizedFace.position.y-this.curMesh.parasitizedFace.size.y/2) {
                    	this.parasitizedPoint.y=this.curMesh.parasitizedFace.position.y-this.curMesh.parasitizedFace.size.y/2+this.curMesh.size.y/2;
                	}
				}
				else if(this.curMesh.position.z<this.curMesh.parasitizedFace.position.z){
	                this.parasitizedPoint.z=this.curMesh.parasitizedFace.position.z-this.curMesh.size.z/2;
	                if(this.parasitizedPoint.x+this.curMesh.size.x/2>this.curMesh.parasitizedFace.position.x+this.curMesh.parasitizedFace.size.x/2) {
	                    this.parasitizedPoint.x=this.curMesh.parasitizedFace.position.x+this.curMesh.parasitizedFace.size.x/2-this.curMesh.size.x/2;
	                } else if(this.parasitizedPoint.x-this.curMesh.size.x/2<this.curMesh.parasitizedFace.position.x-this.curMesh.parasitizedFace.size.x/2) {
	                    this.parasitizedPoint.x=this.curMesh.parasitizedFace.position.x-this.curMesh.parasitizedFace.size.x/2+this.curMesh.size.x/2;
	                }
	                if(this.parasitizedPoint.y+this.curMesh.size.y/2>this.curMesh.parasitizedFace.position.y+this.curMesh.parasitizedFace.size.y/2) {
	                    this.parasitizedPoint.y=this.curMesh.parasitizedFace.position.y+this.curMesh.parasitizedFace.size.y/2-this.curMesh.size.y/2;
	                } else if(this.parasitizedPoint.y-this.curMesh.size.y/2<this.curMesh.parasitizedFace.position.y-this.curMesh.parasitizedFace.size.y/2) {
	                    this.parasitizedPoint.y=this.curMesh.parasitizedFace.position.y-this.curMesh.parasitizedFace.size.y/2+this.curMesh.size.y/2;
	                }
				}
				
				

                // hasInsectorOnXY();
                break;

        }

        var ctrMax=new THREE.Vector3(this.curMesh.size.x/2+this.parasitizedPoint.x,this.curMesh.size.y/2+this.parasitizedPoint.y,this.curMesh.size.z/2+this.parasitizedPoint.z);
        var ctrMin=new THREE.Vector3(this.parasitizedPoint.x-this.curMesh.size.x/2,this.parasitizedPoint.y-this.curMesh.size.y/2,this.parasitizedPoint.z-this.curMesh.size.z/2);
        for(var i in this.scene.children) {
            var g=this.scene.children[i];
            // var g=onMesh;
            if( g!=this.curMesh && g instanceof THREE.Mesh &&
            g.gTypeId!='floor' && g.gTypeId!='ceiling' &&
            g.gTypeId!='fwall' && g.gTypeId!='bwall' &&
            g.gTypeId!='lwall' && g.gTypeId!='rwall'  &&
            g.gTypeId!='structure' && g.name!='plane' &&
            g.gTypeId!='xwall' && g.gTypeId!='ywall') {
                var gMax=new THREE.Vector3(g.position.x+g.size.x/2,g.position.y+g.size.y/2,g.position.z+g.size.z/2);
                var gMin=new THREE.Vector3(g.position.x-g.size.x/2,g.position.y-g.size.y/2,g.position.z-g.size.z/2);
                if(Math.min(ctrMax.x,gMax.x)>Math.max(ctrMin.x,gMin.x) &&
                Math.min(ctrMax.y,gMax.y)>Math.max(ctrMin.y,gMin.y) &&
                Math.min(ctrMax.z,gMax.z)>Math.max(ctrMin.z,gMin.z)) {
                    return true;
                }
            }
        }
        this.parasitizedPoint
        this.curMesh.position.copy(this.parasitizedPoint);
        this.onMesh=onMesh;
        if(this.curMesh.gTypeId=="10") {
            // switch(this.curMesh.parasitizedFace.gTypeId){
            // case 'fwall':
            // this.parasitizedPoint.z=this.curMesh.parasitizedFace.position.z;
            // break;
            // case 'bwall':
            // this.parasitizedPoint.z=this.curMesh.parasitizedFace.position.z;
            // break;
            // case "lwall":
            // this.parasitizedPoint.x=this.curMesh.parasitizedFace.position.x;
            // break;
            // case "rwall":
            // this.parasitizedPoint.x=this.curMesh.parasitizedFace.position.x;
            // break;
            // }
            // this.curMesh.position.copy(this.parasitizedPoint);
            this.curMesh.position.y=this.curMesh.size.y/2+4/2;
            return;

        }

        return false;

    }

};

Facility.prototype.remove= function() {
    this.scene.remove(this.curMesh);
    this.curMesh=null;
    this.touchPoint.set(0,0,0);
    this.parasitizedPoint=null;
    this.hostInfo=null;
};
Facility.prototype.addMesh = function(geometry, scale, position, rotation, material,name, description, _id, gTypeId,roomId,goodsName) {
    geometry.computeBoundingBox();
    mesh = new THREE.Mesh(geometry, material);
    mesh.scale.x = mesh.scale.y = mesh.scale.z = scale;
    mesh.position.x = position.x;
    mesh.position.y = position.y;
    mesh.position.z = position.z;
    mesh.rotation.x = rotation.x;
    mesh.rotation.y = rotation.y;
    mesh.rotation.z = rotation.z;
    mesh.overdraw = true;
    mesh.doubleSided = false;
    mesh.gTypeId = gTypeId;
    mesh.roomId=roomId;
    mesh.name=name;
    mesh.goodsName=goodsName;
    mesh._id = _id;
    var today = new Date();
    // var rndId = Math.ceil((today.getTime() * 9301 + 49297) % 233280 / 233280.0 * 511555);
    var rndId=RoomUtil.random();
    mesh.rndId = rndId;
    mesh.description = description;
    mesh.size = geometry.boundingBox.max.subSelf(geometry.boundingBox.min);
    // mesh.matrixAutoUpdate = true;
    mesh.updateMatrix();

    // if(name=='room')
    //     this.scene.add(mesh);
    // else
    //     this.group.add(mesh);
    return mesh;

};
Facility.prototype.createScene= function ( geometry, x, y, z, b ) {

    zmesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial() );
    zmesh.position.set( x, y, z );
    zmesh.scale.set( 3, 3, 3 );
    this.scene.add( zmesh );

    return zmesh;
};
Facility.prototype.locateOnHost= function(mouse2D,onMesh) {
    if(!this.curMesh)
        return;
    projector = new THREE.Projector();
    ray = projector.pickingRay(mouse2D.clone(),this.camera );
    //此处应优化为ray.intersect(room );
    // var intersects = ray.intersectObjects(this.scene.children );
    
     var intersects = ray.intersectObjects([onMesh] );
    this.parasitizedPoint=this.getTouchPointOnHost(intersects);

};
Facility.prototype.getTouchPointOnHost= function(intersects) {
    for( i = 0; i < intersects.length; i++ ) {
        intersector = intersects[ i ];
        if(!this.curMesh.parasitizedFace) {
            console.warn(this.curMesh.parasitizedFace);
        } else {
            if(intersector.object.gTypeId==this.curMesh.parasitizedFace.gTypeId) {

                return intersector.point;
            }
        }
    }
    return null;
};
Facility.prototype.update= function() {
    if(this.curMesh &&this.curMesh.isRotate) {
        this.curMesh.rotation.y += ( this.targetRotation - this.curMesh.rotation.y ) * 0.05;
        // console.warn( this.curMesh.geometry.boundingBox.max.subSelf(this.curMesh.geometry.boundingBox.min));
    }
};
